package com.jixing.rengran.huaji.retrofit.helper;

import com.jixing.rengran.huaji.app.Const;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by l5544 on 2017/11/7.
 */

public class RetrofitHelper {
    private final OkHttpClient mClient;
    private Retrofit mRetrofit;

    private RetrofitHelper(){

        mClient = OkHttpClientHelper.getInstance().getOkHttpClient();
    }

    private static RetrofitHelper helper;

    //单例 保证对象唯一
    public static RetrofitHelper getInstance(){
        if(helper==null){
            synchronized (RetrofitHelper.class){
                if(helper==null){
                    helper = new RetrofitHelper();
                }
            }
        }
        return helper;
    }

    //获取Retrofit对象
    public Retrofit getRetrofit(){

        if(mRetrofit==null) {
            mRetrofit = new Retrofit.Builder()
                    .baseUrl(Const.URL.BASE)
                    .addConverterFactory(GsonConverterFactory.create())  //添加Gson支持
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())  //添加RxJava支持
                    .client(mClient)                                            //关联okhttp
                    .build();
        }
        return mRetrofit;
    }
}
